#!./perl

# ------------------------------------------------------------

# edit_info.pl, by Jean-Pierre Girard (jpg@corrado.sun.com)
# This allows the user to edit or create the _info file
# necessary for the CDware.

# ------------------------------------------------------------

# Bugs and other fixes

# ------------------------------------------------------------
%Categories = (
"archive", "Archiving, Storage Management & Backup Solutions"
,"business", "Business Solutions"
, "cad_cae", "CAD/CAE"
, "catalogs", "Catalogs"
, "client_server", "Client/Server Solutions"
, "desktop_apps", "Desktop Applications"
, "doc_mgt", "Document Management"
, "epubs", "Electronic Publishing"
, "graphics_imaging", "Graphics and Imaging"
, "info_mgt", "Information Management"
, "multimedia", "Multimedia"
, "networking", "Networking"
, "security", "Security"
, "sw_eng", "Software Engineering"
, "sys_admin", "System Administration"
, "telco", "Telecommunications"
, "training", "Training"
, "Hardware", "Hardware"
, "Services", "Services"
, "internet", "Internet"
, "Info", "Info"
);

@PlatOrder = ("S2", "S1", "S2x86", "S2PPC");

%Platforms = (
"S1", "Solaris 1 (SunOS 4)"
, "S2", "Solaris 2 SPARC"
, "S2x86", "Solaris 2 Intel"
, "S2PPC", "Solaris 2 PowerPC"
);

sub IsSelected
{
	local($KeyCat) = @_;
	@fields = split(/\s+/, $ChosenCat);
	foreach $idem (@fields) {
		if ($idem eq $KeyCat) {
			"SELECTED";
			last;
		}
	}
}

sub UnCapitalizedSort
{
	($al = $a) =~ tr/A-Z/a-z/;
	($bl = $b) =~ tr/A-Z/a-z/;
	$al cmp $bl;
}

##############################################################################
# WriteForm: Write the form to be filled by the user
##############################################################################
sub WriteForm
{
	$~ = "MAININFO";
	write STDOUT;

	$~ = "PLATINFO";
	#foreach $NickPlat (sort UnCapitalizedSort keys(%Platforms)) {
	foreach $NickPlat (@PlatOrder) {
		write STDOUT;
	}

	$~ = "MIDINFO";
	write STDOUT;

	$~ = "CATINFO";
	foreach $NickCat (sort UnCapitalizedSort keys(%Categories)) {
		$CatName = $Categories{$NickCat};
		unless ($CatName =~ /^!.*/) {
			write STDOUT;
		}
	}

	$~ = "ENDINFO";
	write STDOUT;
}
##############################################################################
# CreateHTML: Create an HTML page initialized with the _info file content
##############################################################################
sub CreateHTML
{
	if (-s $INFOFILENAME && open (INFOFILE, $INFOFILENAME)) {
		$LINE=<INFOFILE>;
		($Company, $Description, $Platform, $ChosenCat, $WebSite, $Email, $HomePageName) = split(/\^/, $LINE);
		$Company="value=\"" . $Company . "\"";
		$Description="value=\"" . $Description . "\"";
		$WebSite="value=\"" . $WebSite . "\"";
		$Email="value=\"" . $Email . "\"";
	
		@fields = split(/\s+/, $Platform . " " . $ChosenCat);
		foreach $idem (@fields) {
			$Checked{$idem} = "CHECKED"
		}
		while (<INFOFILE>) {
			$Product=$_;
			chop($Product);
			chop($Product);
			push(@Products, "value=\"" . $Product . "\"");
		}
	} else {
	}

	$HomePageName="value=\"" . ($HomePageName =~ /^ *$/?"index.html":$HomePageName) . "\"";

	$~ = "HTMLHEADER";
	write STDOUT;

	&WriteForm();
}

sub url_decode
{
 
#       Decode a URL encoded string or array of strings
#               + -> space
#               %xx -> character xx
 
 
    foreach (@_) {
        tr/+/ /;
        s/%0a //g;
        s/\^//g;
        s/%(..)/pack("c",hex($1))/ge;
    }
    @_;
}

sub Init
{
	local($comp, $errMes) = @_;
	$comp=$rqpairs{$comp};
	if ($comp eq "") {
		$Status = &PrintErr($errMes);
	}
	$comp;
}

sub WriteInfo
{
	local($old);
	$~ = "HTMLHEADER";
	write STDOUT;

	@names = &url_decode(split(/[&=]/, $request));
	%rqpairs = @names;
	open (INFOFILE, "> $INFOFILENAME");
	$old = select(INFOFILE);

	$Company = &Init("CompanyName", "MisCompany");
	$Description = &Init("Description", "MisDescription");
	$Description =~ s/^[- 	]*/- /;
	$WebSite = $rqpairs{"WebSite"};
	$WebSite =~ s/^\///;
	$WebSite =~ s/^http:\/\///;
	#$WebSite =~ s/\/$//;
	$Email = $rqpairs{"Email"};
	$HomePageName = &Init("Home", "MisHome");;
	$Products[0] = &Init("Product1", "MisProduct1");
	$Products[1] = $rqpairs{"Product2"};
	$Products[2] = $rqpairs{"Product3"};

	substr($Company, 0, 1) =~ tr/a-z/A-Z/;
	print $Company . "^" . $Description . "^";
	print ($rqpairs{"hasSlide"} eq "yes" ? "demo ":"");
	print ($rqpairs{"hasTryIt"} eq "yes" ? "testdrive ":"");
	print ($rqpairs{"hasInstall"} eq "yes" ? "install":"");
	print "^";
	%RevCat = reverse %Categories;
	%RevPlat = reverse %Platforms;
	$foundCat = $foundPlat = 0;
	$ChosenCat = "";
	for ($iname = 0; $iname < @names; $iname+=2) {
		if ($names[$iname] eq "Menu_Cat") {
			$ChosenCat = $ChosenCat . $RevCat{$names[$iname + 1]} . " ";
			print $RevCat{$names[$iname + 1]} . " ";
			$foundCat++;
		} elsif ($names[$iname] eq "Menu_Plat") {
			$buf = $names[$iname + 1];
			$buf =~ s/ *$//;
			$Checked{$RevPlat{$buf}} = "CHECKED";
			print $RevPlat{$buf} . " ";
			$foundPlat++;
		}
	}
	if ($foundPlat == 0) {
		$Status = &PrintErr("MisPlat");
	}
	if ($foundCat == 0) {
		$Status = &PrintErr("MisCat");
	} elsif ($foundCat > 3) {
		$Status = &PrintErr("TooManyCat");
	}
	$WebSite =~ s/[ 	]//g;
	$Email =~ s/[ 	]//g;
	$HomePageName =~ s/[ 	]//g;
	print "^$WebSite^$Email^$HomePageName^\n";
	print "$Products[0]^\n";
	if ($Products[1] ne "") {
		print "$Products[1]^\n";
	}
	if ($Products[2] ne "") {
		print "$Products[2]^\n";
	}
	close(INFOFILE);
	select($old);
	if ($Status == 1) {
		$Company="value=\"" . $Company . "\"";
		$Description="value=\"" . $Description . "\"";
		$WebSite="value=\"" . $WebSite . "\"";
		$Email="value=\"" . $Email . "\"";
		$HomePageName="value=\"" . $HomePageName . "\"";
		($rqpairs{"hasSlide"} eq "yes" ? $Checked{"demo"} = "CHECKED":null);
		($rqpairs{"hasTryIt"} eq "yes" ? $Checked{"testdrive"} = "CHECKED":null);
		($rqpairs{"hasInstall"} eq "yes" ? $Checked{"install"} = "CHECKED":null);
		for ($iprod = 0; $iprod < 3; $iprod++) {
			$Products[$iprod]= "value=\"" . $Products[$iprod] . "\"";
		}
		&WriteForm();
	} else {
		$~ = "GOODRESULT";
		write STDOUT;
	}
}

##############################################################################
# Main block
##############################################################################
require 'devinfo.pl';
require 'tools.pl';
$HomeDir = &GetDevinfo("dir");
$INFOFILENAME = "$HomeDir/_info";

if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
} elsif ($ENV{'REQUEST_METHOD'} eq "GET" ) {
	$request = $ENV{'QUERY_STRING'};
}
$Checked{"S2"} = "CHECKED";
if ($ARGV[0] eq "HTML") {
	&CreateHTML;
} elsif ($request eq "") {
	print "no request";
} else {
	&WriteInfo;
}

################################################################################
# Define FORMATS (no more code)
################################################################################
format HTMLHEADER =
Content-type: text/html


<html>
<title>Developer's Kit: edit information</title>
<body>
.

format MAININFO =
<h1>Please enter this information and click on "Ok":</h1>
<h4>Currently working with <b>"@*
$HomeDir
" directory</h4>
#<p>You 're currently editing <b>@* 
#$INFOFILENAME
</b><form method="post"  ACTION="http://localhost:7999/cgi-bin/edit_info.pl">
#<h4>CDware _info file: </h4>
<ul><IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_red.gif">: necessary
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_green.gif">: Optional.</ul>
<pre><IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_red.gif">Company name:       <input type="text" 
~@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Company
 name="CompanyName" size="25" maxlength=50>
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_red.gif">Brief description:  <input type="text"
~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Description
 name="Description" size=62 MAXLENGTH=160>
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_red.gif">First Product Name: <input type="text"
~@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Products[0]
 name="Product1" size="35" maxlength=50>
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_green.gif">Second Product Name:<input type="text"
~@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Products[1]
 name="Product2" size="35" maxlength=50>
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_green.gif">Third Product Name: <input type="text"
~@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Products[2]
 name="Product3" size="35" maxlength=50><br>
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_green.gif">Address of your web site: http://<input type="text"
~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$WebSite
 name="WebSite" size=28 MAXLENGTH=160>/
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_green.gif">Email for information request:   <input type="text"
~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Email
 name="Email" size=28 MAXLENGTH=160>
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_red.gif">Filename of your home page:      <input type="text"
~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$HomePageName
 name="Home" size=28 MAXLENGTH=160>
</pre>
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_red.gif">Specify if there will be a Slide-show or a version of your product available for evaluation in your submission:
<ul><ul>
<input type="checkbox" NAME="hasSlide" VALUE="yes" @<<<<<<<<< size="8" >Slide-show available<br>
$Checked{"demo"}
<input type="checkbox" NAME="hasTryIt" VALUE="yes" @<<<<<<<<< size="8" >Trial software available<br>
$Checked{"testdrive"}
<input type="checkbox" NAME="hasInstall" VALUE="yes" @<<<<<<<<<< size="8" >Unlockable available<br>
$Checked{"install"}
<IMG ALIGN=middle src="file:///tmp/httpd/.products/DEV/images/finger.gif"><b>Note:</b> If you do NOT have any of the above, please continue.<br>
</ul></ul>
<p><IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_red.gif">Specify which platforms you're supporting (at least one):
<ul><ul>
.

format PLATINFO =
<input type="checkbox" NAME="Menu_Plat" VALUE="@<<<<<<<<<<<<<<<<<<" @<<<<<<<<<< size="8" >@<<<<<<<<<<<<<<<<<<< <br>
$Platforms{$NickPlat}, $Checked{$NickPlat}, $Platforms{$NickPlat}
.

format MIDINFO =
</ul></ul>
<IMG src="file:///tmp/httpd/.products/DEV/images/bullet_ball_red.gif">Choose one to three categories:<br>
<p><center><SELECT NAME="Menu_Cat" size="10" MULTIPLE>
.

format CATINFO =
<OPTION @<<<<<<<<> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
&IsSelected($NickCat), $CatName
.

format ENDINFO =
</SELECT></center><br>
<P><center>
<input type="submit" VALUE="Ok" >
<input type="Reset" VALUE="Cancel" >
</center>
</FORM>
<hr><h5><p><A HREF=file:///tmp/httpd/.products/CDware/Help/SMICopyright.html>Copyright</A> 1995 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA 94043 USA.  All rights reserved.</h5><br></pre>
<a href=file:///tmp/httpd/.products/DEV/credits.html>Credits</a>
</b>
</body>
</html>
.


format GOODRESULT =
<h1><app class="ImageLoopItem" img="file:///tmp/httpd/.products/DEV/images/duke" width=55 height=68>Congratulations! </h1>
<p>You completed the first step of the creation of your submssion to the CDware.
<p>You can edit this information again at any time from the main menu !
<h4><p><A HREF="http://localhost:7999/cgi-bin/mainmenu.pl? ">Go to main menu</A> </h4><br>
#<hr><h5><p><A HREF=file:///tmp/httpd/.products/CDware/Help/SMICopyright.html>Copyright</A> 1995 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA 94043 USA.  All rights reserved.</h5><br></pre>
#<a href=file:///tmp/httpd/.products/DEV/credits.html>Credits</a>
</body>
</html>
.
